# -*- coding: utf-8 -*-
"""
Created on Sat Nov 24 21:31:10 2018

@author: Sz-wyz
"""
from sklearn.datasets.samples_generator import make_blobs
from matplotlib import pyplot as plt
import numpy as np
from sklearn.neighbors import KNeighborsClassifier

#生成数据
centers = [[-2,2],[2,2],[0,4]]
#以centers为中心生成60个点，随机数种子0，标准差0.6
X,y = make_blobs(n_samples=60,centers=centers,random_state=0,
                 cluster_std=0.6)

#画出数据
plt.figure(figsize=(16,10),dpi=45)
c = np.array(centers)
plt.scatter(X[:,0],X[:,1],c=y,s=100,cmap='cool')#画出样本
plt.scatter(c[:,0],c[:,1],s=100,marker='^',c='orange')#画出中心店   
plt.show()

#模型训练
k = 5
clf = KNeighborsClassifier(n_neighbors=k)
clf.fit(X,y)
#进行预测
X_sample = [0,2]
X_sample = np.array(X_sample).reshape(1, -1)
y_sample = clf.predict(X_sample)
neighbors = clf.kneighbors(X_sample,return_distance=False)#返回邻居

#画出示意图
plt.figure(figsize=(16,10),dpi=50)
plt.scatter(X[:,0],X[:,1],c=y,s=100,cmap='cool')#样本
plt.scatter(c[:,0],c[:,1],s=100,marker='^',c='k')#中心点
plt.scatter(X_sample[0][0],X_sample[0][1],marker='x',
            s=100,cmap='cool')
for i in neighbors[0]:
    plt.plot([X[i][0],X_sample[0][0]],[X[i][1],X_sample[0][1]],'k--',
            linewidth=0.6)#预测点和最近的五个样本点的连线
plt.show()











